#include <stdbool.h>
#include <aos/kv.h>
#include <yoc/partition.h>
#include <yoc/init.h>
#include <drv/pin.h>
#include <yoc/at_port.h>
#include <devices/w800.h>
#include <devices/drv_snd_alkaid.h>
#include "app_main.h"
#include "board.h"
#include "app_kv.h"
#include "app_weather.h"


#define TAG "init"

#define DEBUG_WIFI                   (STD_ON)

netmgr_hdl_t app_netmgr_hdl;
extern at_channel_t spi_channel;
/* Wifi设置相关 */
static uint8_t user_wifi_name_len = 11;    /* wifi名长度 */
static char user_wifi_name[32] = {"hehungphone"};    /* wifi名字 */   
static uint8_t user_wifi_pwd_len = 10;   /* wifi密码长度 */
static char user_wifi_pwd[64] = {"1234543210"};  /* wifi密码 */
static uint8_t wifi_config_flg = 0;


static uint8_t Wifi_ConfigFromKv(void);


netmgr_hdl_t Net_GetnetMgrHdl(void)
{
	return app_netmgr_hdl;
}

static uint8_t Wifi_ConfigFromKv(void)
{
    if ((1 == Kv_GetSaveStatus(KV_WIFI_SSID_S)) && 
        (1 == Kv_GetSaveStatus(KV_WIFI_PWD_S)) &&
        (1 == Kv_GetSaveStatus(KV_WIFI_SSID_LEN)) &&
        (1 == Kv_GetSaveStatus(KV_WIFI_PWD_LEN)))
    {
        user_wifi_name_len = Kv_ReadData(KV_WIFI_SSID_LEN);
        user_wifi_pwd_len = Kv_ReadData(KV_WIFI_PWD_LEN);
        (void)Kv_ReadDataString(KV_WIFI_SSID_S, user_wifi_name, user_wifi_name_len);
        (void)Kv_ReadDataString(KV_WIFI_PWD_S, user_wifi_pwd, user_wifi_pwd_len);

        // printf("\n%s,%d,%s,%d\n", user_wifi_name, user_wifi_name_len, user_wifi_pwd, user_wifi_pwd_len);
        wifi_config_flg = 1;
        return 1;
    }
    else
    {
        wifi_config_flg = 0;
        return 0;
    }

}

void network_init(void)
{
    w800_wifi_param_t w800_param;
    /* init wifi driver and network */
    w800_param.reset_pin      = PA21;
    w800_param.baud           = 1*1000000;
    w800_param.cs_pin         = PA15;
    w800_param.wakeup_pin     = PA25;
    w800_param.int_pin        = PA22;
    w800_param.channel_id     = 0;
    w800_param.buffer_size    = 4*1024;

    wifi_w800_register(NULL, &w800_param);
//    w800_software_reset();
    app_netmgr_hdl = netmgr_dev_wifi_init();

    if (app_netmgr_hdl) 
    {
        utask_t *task = utask_new("netmgr", 2 * 1024, QUEUE_MSG_COUNT, AOS_DEFAULT_APP_PRI);
        netmgr_service_init(task);
        if (1 == Wifi_ConfigFromKv())
        {
            /* 如果KV中已经存在了wifi名称以及密码，则直接使用 */
            netmgr_config_wifi(app_netmgr_hdl, user_wifi_name, user_wifi_name_len, user_wifi_pwd, user_wifi_pwd_len);
        }
        else
        {

        }
        netmgr_start(app_netmgr_hdl);
    }
}

void board_yoc_init(void)
{
    board_init();
    event_service_init(NULL);
    console_init(CONSOLE_UART_IDX, 115200, 512);
    ulog_init();
    aos_set_log_level(AOS_LL_DEBUG);

    int ret = partition_init();
    if (ret <= 0) {
        LOGE(TAG, "partition init failed");
    } else {
        LOGI(TAG, "find %d partitions", ret);
    }

    aos_kv_init("kv");
    Kv_Init();
    
    snd_card_alkaid_register(NULL);
    network_init();

    board_cli_init();
}

/* 存储数据到KV中 */
void Wifi_SaveData2Kv(char *ssid, uint8_t ssid_l, char *pwd, uint8_t pwd_l)
{
    Kv_WriteDataString(KV_WIFI_SSID_S, ssid, ssid_l);
    Kv_WriteDataString(KV_WIFI_PWD_S, pwd, pwd_l);
    Kv_WriteData(KV_WIFI_SSID_LEN, ssid_l);
    Kv_WriteData(KV_WIFI_PWD_LEN, pwd_l);
    // printf("\n%s,%d,%s,%d\n", ssid, ssid_l, pwd, pwd_l);
    /* 配置好了之后重新初始化w800 */
    network_init();
}

/* 获取配置信息 */
uint8_t Wifi_SetConfigInfo(char *ssid, uint8_t *ssid_l, char *pwd, uint8_t *pwd_l)
{
    uint8_t i;

    if (1 == wifi_config_flg)
    {
        for (i=0; i<64; i++)
        {
            if (i < 32)
                ssid[i] = '\0';
            pwd[i] = '\0';
        }
        strncpy(ssid, user_wifi_name, user_wifi_name_len);
        // ssid = user_wifi_name;
        *ssid_l = user_wifi_name_len;
        strncpy(pwd, user_wifi_pwd, user_wifi_pwd_len);
        // pwd = user_wifi_pwd;
        *pwd_l = user_wifi_pwd_len;

        return 1;
    }
    else
    {
        return 0;
    } 
}